<?php defined('SYSPATH') or die('No direct script access.');

/**
 * @file city.php
 * @brief 
 * @author Lionel Tsai
 * @version 1.0
 * @date 2012-07-25
 */


class Controller_City extends Controller_Rest {
	public function before()
	{
		parent::before();
	}

	/**
	 * @brief 获取指定ID的类型
	 *
	 * @param $term_id
	 *
	 * @return $this 
	 */
	protected function get_objc($term_id)
	{
		$term = ORM::factory('city', $term_id);
		if(!$term->term_id or $term_id<=54 or $term_id>=417)
		{
			$ret['flag'] = 0;
			$ret['num'] = 0;
			$ret['description'] = 'City not found!';
			$json_ret = json_encode($ret);
			$this->response->body($json_ret)->status(400);
			return;
		}
		$res = json_encode($term->as_array(CITY_CUSGET));
		$this->response->body($res)->status(200);
	}
	/**
	 * @brief 获取指定起止位置的类型
	 *
	 * @return $this 
	 */
	protected function get_objc_list()
	{
		/** 获取起止数*/
		$start = filter_input(INPUT_GET, 'start', FILTER_VALIDATE_INT);
		$pos = filter_input(INPUT_GET, 'pos', FILTER_VALIDATE_INT);
		$order = filter_input(INPUT_GET, 'order');
		$province_id = filter_input(INPUT_GET, 'province_id', FILTER_VALIDATE_INT);


		$term = ORM::factory('city');
	
		//$start = ($start) ? $start : 0;
		//$pos = ($pos) ? $pos : PHP_INT_MAX;

		$order = ($order and ($order == 'asc' or $order == 'desc')) ? $order : 'asc';
		$province_id = ($province_id) ? $province_id : 1;//1为全部
		 
		/** 获取指定省份(if have)指定起止位置(if have)的所有城市信息*/
		if($province_id !== 1) 
		{
		 //获得对应的term_taxonomy的记录
			$type = ORM::factory('type');
			$types = $type->where('parent', '=', $province_id)->find_all();

			$total_count =  $type->where('parent', '=', $province_id)->count_all();
			$start = ($start) ? $start : 0;
			$pos = ($pos) ? $pos :$total_count;
			$cityids = array();
			foreach($types as $typ) {
				array_push($cityids, $typ->term_id);
			}
			//var_dump($cityids);
			$term
			 ->and_where('term_id', 'IN',$cityids )
			 ->order_by(DB::expr("CONVERT(name USING GBK )"),$order);
			
			
			$terms = $term
					->where('term_id', '>', 54)
					->and_where('term_id', '<', 417)
					->offset($start)
					->limit($pos)->find_all();
		}
		else
		{
			$total_count = PHP_INT_MAX;
			$start = ($start) ? $start : 0;
			$pos = ($pos) ? $pos :$total_count;
			$term->order_by(DB::expr("CONVERT(name USING GBK )"),$order);
			$terms = $term
				->where('term_id', '>', 54)
				->and_where('term_id', '<', 417)
				->offset($start)
				->limit($pos)
				->find_all();
		}	
		
		if(!$terms->count())
		{
			$ret['flag'] = 0;
			$ret['num'] = 0;
			$ret['description'] = 'City not found!';
			$json_ret = json_encode($ret);
			$this->response->body($json_ret)->status(206);
		}
		else
		{
			/** 返回条数及列表*/
			$res['flag'] = 1;			
			$res['order'] = $order;
			$res['num'] = $terms->count();
			$res['totalcount'] = $total_count;
			
			$res['list'] = array();
			foreach($terms as $tem)
			{
				$res['list'][] = $tem->as_array(CITY_CUSGET);
			}
			$json_res = json_encode($res);
			$this->response->body($json_res)->status(200);
		}
	}

	/**
	 * @brief 通过城市名称获取id
	 *
	 * @return 
	 */
	public function action_cityid_get() {
		//获取城市名称
		$pname = filter_input(INPUT_GET, 'pname');
		
		$query = DB::select()->from('terms')
		 ->where('name', 'like', $pname.'%')
		 ->and_where('term_id', '>', 54)
		 ->and_where('term_id', '<', 417);
		$terms = $query->execute();	
		
		if(!$terms->count())
		{
			$ret['flag'] = 0;
			$ret['num'] = 0;
			$ret['description'] = 'City with name not found!';
			$json_ret = json_encode($ret);
			$this->response->body($json_ret)->status(206);
		} else {
			/** 返回条数及列表*/
			$res['flag'] = 1;			
			$res['num'] = $terms->count();
			
	/*	foreach($terms as $tem)
			{
				$res['list'][] = $tem->as_array(CITY_CUSGET);
			}*/
			$res['cityid'] = $terms[0]['term_id'];
			$res['cityname'] = $terms[0]['name'];
			
			$json_res = json_encode($res);
			$this->response->body($json_res)->status(200);
		}
		
				
	}
}
